package com.itcam.designmodule.adapter_;

import javax.swing.*;
import java.awt.event.*;

/**
 * Author:Cammy.Wu
 * Date:2024-05-11
 * Description: 适配器设计模式：解决接口与接口实现的矛盾问题
 */

public class AdapterTesst01 {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(null);

        JButton btn = new JButton("按钮");
        btn.setBounds(0, 0, 100, 100);
        frame.getContentPane().add(btn);

        //--------------------------------------------------------------------------------------------------------------

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("点击了...");
            }
        });

        // 取消按钮焦点
        btn.setFocusable(false);

        //--------------------------------------------------------------------------------------------------------------

        // KeyAdapter是KeyListener的适配器类
        frame.addKeyListener(new KeyAdapter() {
            /**
             * Invoked when a key has been pressed.
             *
             * @param e
             */
            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("键盘按下了...");
            }
        });

        frame.setVisible(true);
    }
}
